cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

set(CMAKE_INSTALL_PREFIX "/home/lizr/GridFinal/build/test")

if(DEFINED CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose build type")
else()
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose build type")
endif()

project(cmsh CXX C)

# require C++14 and request C99
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)

set(DEFAULT ON CACHE INTERNAL "Default value for enabled-by-default options")

macro(opt OPTION HELP VALUE)
  option(ENABLE_${OPTION} ${HELP} ${VALUE})
  set(OPT_TEXI "${OPT_TEXI}\n@item ENABLE_${OPTION}\n${HELP} (default: ${VALUE})")
endmacro()

opt(3M "Enable proprietary 3M extension" OFF)
opt(ALGLIB "Enable ALGLIB (used by some mesh optimizers)" ${DEFAULT})
opt(ANN "Enable ANN (used for fast point search in mesh/post)" ${DEFAULT})
opt(BAMG "Enable Bamg 2D anisotropic mesh generator" ${DEFAULT})
opt(BLAS_LAPACK "Enable BLAS/Lapack for linear algebra (if Eigen if disabled)" OFF)
opt(BLOSSOM "Enable Blossom algorithm (needed for full quad meshing)" ${DEFAULT})
opt(BUILD_LIB "Enable 'lib' target for building static library" OFF)
opt(BUILD_SHARED "Enable 'shared' target for building shared library" OFF)
opt(BUILD_DYNAMIC "Enable dynamic executable (linked with shared library)" OFF)
opt(BUILD_ANDROID "Enable Android NDK library target (experimental)" OFF)
opt(BUILD_IOS "Enable iOS library target (experimental)" OFF)
opt(CGNS "Enable CGNS import/export (experimental)" ${DEFAULT})
opt(CGNS_CPEX0045 "Enable high-order CGNS import/export following CPEX0045 (experimental)" OFF)
opt(CAIRO "Enable Cairo to render fonts (experimental)" ${DEFAULT})
opt(PROFILE "Enable profiling compiler flags" OFF)
opt(DINTEGRATION "Enable discrete integration (needed for levelsets)" ${DEFAULT})
opt(DOMHEX "Enable experimental DOMHEX code" ${DEFAULT})
opt(EIGEN "Enable Eigen for linear algebra (instead of Blas/Lapack)" ON)
opt(GEOMETRYCENTRAL "Enable geometry-central library (experimental)" ${DEFAULT})
opt(GETDP "Enable GetDP solver (linked as a library, experimental)" ${DEFAULT})
opt(GMM "Enable GMM linear solvers (simple alternative to PETSc)" ${DEFAULT})
opt(GMP "Enable GMP for Kbipack (advanced)" ON)
opt(GRAPHICS "Enable building graphics lib even without GUI (advanced)" OFF)
opt(HXT "Enable HXT library (for reparametrization and meshing)" ${DEFAULT})
opt(KBIPACK "Enable Kbipack (neeeded by homology solver)" ${DEFAULT})
opt(MATHEX "Enable Mathex expression parser (used by plugins and options)" ${DEFAULT})
opt(MED "Enable MED mesh and post file formats" ${DEFAULT})
opt(MESH "Enable mesh module" ${DEFAULT})
opt(MESQUITE "Enable mesquite" ${DEFAULT})
opt(METIS "Enable Metis mesh partitioner" ${DEFAULT})
opt(MMG "Enable Mmg mesh adaptation interface" ${DEFAULT})
opt(MPEG_ENCODE "Enable built-in MPEG movie encoder" ${DEFAULT})
opt(MPI "Enable MPI (experimental, not used for meshing)" OFF)
opt(MSVC_STATIC_RUNTIME "Enable static Visual C++ runtime" OFF)
opt(MUMPS "Enable MUMPS sparse direct linear solver" OFF)
opt(NETGEN "Enable Netgen 3D frontal mesh generator" ${DEFAULT})
opt(NII2MESH "Enable 3D voxel image to a triangulated mesh" ${DEFAULT})
opt(NUMPY "Enable fullMatrix and numpy array conversion for private API" OFF)
opt(PETSC4PY "Enable petsc4py wrappers for petsc matrices for private API" OFF)
opt(OPENACC "Enable OpenACC" OFF)
opt(OPENMP "Enable OpenMP" ${DEFAULT})
opt(OPTHOM "Enable high-order mesh optimization tools" ${DEFAULT})
opt(OS_SPECIFIC_INSTALL "Enable OS-specific (e.g. app bundle) installation" OFF)
opt(OSMESA "Enable OSMesa for offscreen rendering (experimental)" OFF)
opt(P4EST "Enable p4est for enabling automatic mesh size field (experimental)" OFF)
opt(PACKAGE_STRIP "Strip symbols in install packages to reduce install size" ON)
opt(PARSER "Enable GEO file parser (required for .geo/.pos scripts)" ${DEFAULT})
opt(PETSC "Enable PETSc linear solvers (required for SLEPc)" OFF)
opt(PLUGINS "Enable post-processing plugins" ${DEFAULT})
opt(POST "Enable post-processing module (required by GUI)" ${DEFAULT})
opt(POPPLER "Enable Poppler for displaying PDF documents (experimental)" OFF)
opt(PRO "Enable PRO extensions" ${DEFAULT})
opt(QUADMESHINGTOOLS "Enable QuadMeshingTools extensions" ${DEFAULT})
opt(QUADTRI "Enable QuadTri structured meshing extensions" ${DEFAULT})
opt(REVOROPT "Enable Revoropt (used for CVT remeshing)" OFF)
opt(RPATH "Use RPATH in dynamically linked targets" ON)
opt(SLEPC "Enable SLEPc eigensolvers" OFF)
opt(SOLVER "Enable built-in finite element solvers (required for reparametrization)" ${DEFAULT})
opt(SYSTEM_CONTRIB "Use system versions of contrib libraries, when possible" OFF)
opt(TCMALLOC "Enable libtcmalloc (fast malloc that does not release memory)" OFF)
opt(TESTS "Enable tests" ${DEFAULT})
opt(TINYXML2 "Enable TinyXML2 parser (used by XAO reader)" ${DEFAULT})
opt(TOUCHBAR "Enable Apple Touch bar" ${DEFAULT})
opt(UNTANGLE "Enable 2D and 3D UNTANGLER" ${DEFAULT})
opt(VISUDEV "Enable additional visualization capabilities for development purposes" OFF)
opt(VOROPP "Enable voro++ (for hex meshing, experimental)" ${DEFAULT})
opt(WINSLOWUNTANGLER "Enable WinslowUntangler extensions (requires ALGLIB)" ${DEFAULT})
opt(WRAP_JAVA "Generate SWIG Java wrappers for private API" OFF)
opt(WRAP_PYTHON "Generate SWIG Python wrappers for private API (not used by public API)" OFF)
# opt(ZIPPER "Enable Zip file compression/decompression" OFF)

set(CMSH_SHORT_LICENSE "MIT License")

if(CYGWIN OR MSYS)
  # detect if we use the MinGW compilers on Cygwin - if we do, handle the build
  # as a pure Windows build and make cmake find pure Windows import libraries
  # (.lib)
  if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
     CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpmachine
                    OUTPUT_VARIABLE CXX_COMPILER_MACHINE
                    OUTPUT_STRIP_TRAILING_WHITESPACE)
    if(CXX_COMPILER_MACHINE MATCHES "mingw")
      set(WIN32 1)
      add_definitions(-DWIN32)
      set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
      set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".so" ".lib" ".LIB" ".dll" ".DLL" ".dll.a")
    endif()
  endif()
endif()

include(CheckTypeSize)
include(CheckSymbolExists)
include(CheckIncludeFile)
include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)

macro(set_config_option VARNAME STRING)
  set(${VARNAME} TRUE)
  list(APPEND CONFIG_OPTIONS ${STRING})
  message(STATUS "Found " ${STRING})
endmacro()

# check the size of size_t
check_type_size("size_t" SIZEOF_SIZE_T)
if(SIZEOF_SIZE_T EQUAL 8)
  set_config_option(HAVE_64BIT_SIZE_T "64Bit")
endif()

if(ENABLE_RPATH)
  set(CMAKE_MACOSX_RPATH 1)

  # use, i.e. don't skip the full RPATH for the build tree
  set(CMAKE_SKIP_BUILD_RPATH FALSE)

  # when building, don't use the install RPATH already (but later on when
  # installing)
  set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

  set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

  # add the automatically determined parts of the RPATH which point to
  # directories outside the build tree to the install RPATH
  set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

  # the RPATH to be used when installing, but only if it's not a system directory
  list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib"
       isSystemDir)
  if("${isSystemDir}" STREQUAL "-1")
    set(CMAKE_INSTALL_RPATH "\\\$ORIGIN/../lib")
  endif()
else()
  set(CMAKE_MACOSX_RPATH 0)
  set(CMAKE_SKIP_BUILD_RPATH TRUE)
endif()

if(MSVC)
  # remove annoying warning about bool/int cast performance
  set(CMSH_CONFIG_PRAGMAS "#pragma warning(disable:4800 4244 4267)")
  foreach(VAR
          CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
          CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
          CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
          CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
    if(ENABLE_MSVC_STATIC_RUNTIME AND ${VAR} MATCHES "/MD")
      string(REGEX REPLACE "/MD" "/MT" ${VAR} "${${VAR}}")
    endif()
    if(NOT ${VAR} MATCHES "/MP") # enable parallel compilation
      set(${VAR} "${${VAR}} /MP")
    endif()
  endforeach()
endif()

# reduce memory usage of GCC on 32 bit systems
if(NOT HAVE_64BIT_SIZE_T AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  set(CMAKE_CXX_FLAGS
      "${CMAKE_CXX_FLAGS} --param ggc-min-expand=1 --param ggc-min-heapsize=512000")
endif()

if(ENABLE_OPENMP)
  find_package(OpenMP)
  if(OpenMP_FOUND OR OPENMP_FOUND)
    set_config_option(HAVE_OPENMP "OpenMP")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    list(APPEND EXTERNAL_LIBRARIES "${OpenMP_CXX_LIBRARIES}")
  elseif(APPLE AND EXISTS "/opt/local/lib/libomp")
    # official Apple compiler with macports' libomp
    set_config_option(HAVE_OPENMP "OpenMP[MacPorts]")
    set(CMAKE_C_FLAGS
        "${CMAKE_C_FLAGS} -Xpreprocessor -fopenmp -I/opt/local/include/libomp")
    set(CMAKE_CXX_FLAGS
        "${CMAKE_CXX_FLAGS} -Xpreprocessor -fopenmp -I/opt/local/include/libomp")
    list(APPEND EXTERNAL_LIBRARIES "-L/opt/local/lib/libomp -lomp")
  elseif(APPLE AND EXISTS "/usr/local/lib/libomp.dylib")
    # official Apple compiler with homebrew's libomp on Intel
    set_config_option(HAVE_OPENMP "OpenMP[Homebrew]")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Xpreprocessor -fopenmp")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xpreprocessor -fopenmp")
    list(APPEND EXTERNAL_LIBRARIES "-L/usr/local/lib -lomp")
  elseif(APPLE AND EXISTS "/opt/homebrew/opt/libomp")
    # official Apple compiler with homebrew's libomp on M processors
    set_config_option(HAVE_OPENMP "OpenMP[Homebrew]")
    set(CMAKE_C_FLAGS
        "${CMAKE_C_FLAGS} -Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include")
    set(CMAKE_CXX_FLAGS
        "${CMAKE_CXX_FLAGS} -Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include")
    list(APPEND EXTERNAL_LIBRARIES "-L/opt/homebrew/opt/libomp/lib -lomp")
  elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND EXISTS "/usr/local/lib64/libgomp.a")
    # custom static openmp for official linux builds
    set_config_option(HAVE_OPENMP "OpenMP[static]")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Xpreprocessor -fopenmp")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xpreprocessor -fopenmp")
    list(APPEND EXTERNAL_LIBRARIES "/usr/local/lib64/libgomp.a")
  endif()
endif()

if(ENABLE_OPENACC)
  find_package(OpenACC)
  if(OpenACC_C_FOUND AND OpenACC_CXX_FOUND)
    set_config_option(HAVE_OPENACC "OpenACC")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenACC_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenACC_CXX_FLAGS}")
  endif()
endif()

if(ENABLE_PROFILE)
    # Using the perf set of profiling tools doesn't work without the frame
    # pointer and a common optimisation is to remove it
    check_cxx_compiler_flag("-fno-omit-frame-pointer" FNOFP)
    if(FNOFP)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer")
    endif()
endif()

macro(append_cmsh_src DIRNAME FILES)
  foreach(FILE ${FILES})
    list(APPEND LIST ${DIRNAME}/${FILE})
  endforeach()
  set(CMSH_SRC ${CMSH_SRC};${LIST} PARENT_SCOPE)
  set(CMSH_DIRS ${CMSH_DIRS};${DIRNAME} PARENT_SCOPE)
endmacro()

macro(find_all_libraries VARNAME LISTNAME PATH SUFFIX)
  set(${VARNAME})
  list(LENGTH ${LISTNAME} NUM_LIST)
  foreach(LIB ${${LISTNAME}})
    if("${PATH}" STREQUAL "")
      find_library(FOUND_LIB ${LIB} PATH_SUFFIXES ${SUFFIX})
    else()
      find_library(FOUND_LIB ${LIB} PATHS ${PATH} NO_DEFAULT_PATH)
    endif()
    if(FOUND_LIB)
      list(APPEND ${VARNAME} ${FOUND_LIB})
    endif()
    unset(FOUND_LIB CACHE)
  endforeach()
  list(LENGTH ${VARNAME} NUM_FOUND_LIBRARIES)
  if(NUM_FOUND_LIBRARIES LESS NUM_LIST)
    set(${VARNAME})
  endif()
endmacro()

macro(set_compile_flags LISTNAME FLAGS)
  foreach(FILE ${${LISTNAME}})
    get_source_file_property(PROP ${FILE} COMPILE_FLAGS)
    if(PROP)
      set_source_files_properties(${FILE} PROPERTIES COMPILE_FLAGS "${PROP} ${FLAGS}")
    else()
      set_source_files_properties(${FILE} PROPERTIES COMPILE_FLAGS "${FLAGS}")
    endif()
  endforeach()
endmacro()

if(ENABLE_GEOMETRYCENTRAL)
  find_library(GEOMETRYCENTRAL_LIB NAMES geometry-central)
  find_path(GEOMETRYCENTRAL_INC "surface/meshio.h" PATH_SUFFIXES geometrycentral)
  if(GEOMETRYCENTRAL_LIB AND GEOMETRYCENTRAL_INC)
    list(APPEND EXTERNAL_LIBRARIES ${GEOMETRYCENTRAL_LIB})
    list(APPEND EXTERNAL_INCLUDES ${GEOMETRYCENTRAL_INC})
    set_config_option(HAVE_GEOMETRYCENTRAL "geometry-central")
  endif()
endif()

if(ENABLE_EIGEN)
  if(ENABLE_SYSTEM_CONTRIB)
    find_path(EIGEN_INC "Eigen/Dense" PATH_SUFFIXES eigen3)
    if(EIGEN_INC)
      include_directories(${EIGEN_INC})
      set_config_option(HAVE_EIGEN "Eigen")
      set(HAVE_CONTRIB_EIGEN FALSE)
    endif()
  endif()
  if(NOT HAVE_EIGEN AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/eigen)
    include_directories(contrib/eigen)
    set_config_option(HAVE_EIGEN "Eigen[contrib]")
    set(HAVE_CONTRIB_EIGEN TRUE)
  endif()
  # We could also add an option to use BLAS with Eigen
  # add_definitions(-DEIGEN_USE_BLAS)
endif()

if(NOT HAVE_EIGEN AND ENABLE_BLAS_LAPACK)
  if(BLAS_LAPACK_LIBRARIES)
    # use libs as specified in the BLAS_LAPACK_LIBRARIES variable
    set_config_option(HAVE_BLAS "Blas[custom]")
    set_config_option(HAVE_LAPACK "Lapack[custom]")
    set(LAPACK_LIBRARIES ${BLAS_LAPACK_LIBRARIES})
  else()
    if(MSVC)
      if(HAVE_64BIT_SIZE_T)
        set(MKL_PATH em64t/lib)
        set(MKL_LIBS_REQUIRED libguide40 mkl_intel_lp64 mkl_intel_thread mkl_core)
      else()
        set(MKL_PATH ia32/lib)
        set(MKL_LIBS_REQUIRED libguide40 mkl_intel_c mkl_intel_thread mkl_core)
      endif()
      find_all_libraries(LAPACK_LIBRARIES MKL_LIBS_REQUIRED "" ${MKL_PATH})
      if(LAPACK_LIBRARIES)
        set_config_option(HAVE_BLAS "Blas[mkl]")
        set_config_option(HAVE_LAPACK "Lapack[mkl]")
      else()
        set(REFLAPACK_LIBS_REQUIRED lapack blas g2c gcc)
        find_all_libraries(LAPACK_LIBRARIES REFLAPACK_LIBS_REQUIRED "" "")
        if(LAPACK_LIBRARIES)
          set_config_option(HAVE_BLAS "Blas[ref]")
          set_config_option(HAVE_LAPACK "Lapack[ref]")
        endif()
      endif()
    elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
      # on Linux try to find the Intel MKL without a Fortran compiler
      if(HAVE_64BIT_SIZE_T)
        set(MKL_PATH lib/em64t)
      else()
        set(MKL_PATH lib/32)
      endif()
      set(MKL_LIBS_REQUIRED mkl_gf_lp64 iomp5 mkl_gnu_thread mkl_core guide pthread)
      find_all_libraries(LAPACK_LIBRARIES MKL_LIBS_REQUIRED "" ${MKL_PATH})
      if(NOT LAPACK_LIBRARIES)
        # match lapack 9.0 on 64bit
        set(MKL_LIBS_REQUIRED mkl_lapack mkl_em64t guide)
        find_all_libraries(LAPACK_LIBRARIES MKL_LIBS_REQUIRED "" ${MKL_PATH})
      endif()
      if(LAPACK_LIBRARIES)
        set_config_option(HAVE_BLAS "Blas[mkl]")
        set_config_option(HAVE_LAPACK "Lapack[mkl]")
      else()
        # on Linux also try to find ATLAS without a Fortran compiler, because
        # cmake ships with a buggy FindBLAS e.g. on Ubuntu Lucid Lynx
        set(ATLAS_LIBS_REQUIRED lapack f77blas cblas atlas)
        find_all_libraries(LAPACK_LIBRARIES ATLAS_LIBS_REQUIRED "" "")
        if(LAPACK_LIBRARIES)
          set_config_option(HAVE_BLAS "Blas[atlas]")
          set_config_option(HAVE_LAPACK "Lapack[atlas]")
        else()
          # try with generic names
          set(GENERIC_LIBS_REQUIRED lapack blas pthread)
          find_all_libraries(LAPACK_LIBRARIES GENERIC_LIBS_REQUIRED "" "")
          if(LAPACK_LIBRARIES)
            set_config_option(HAVE_BLAS "Blas[generic]")
            set_config_option(HAVE_LAPACK "Lapack[generic]")
            find_library(GFORTRAN_LIB gfortran)
            if(GFORTRAN_LIB)
              list(APPEND LAPACK_LIBRARIES ${GFORTRAN_LIB})
            endif()
          endif()
        endif()
      endif()
    elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
      # on SunOS we know blas and lapack are available in sunperf
      set(LAPACK_LIBRARIES -library=sunperf)
      set_config_option(HAVE_BLAS "Blas[sunperf]")
      set_config_option(HAVE_LAPACK "Lapack[sunperf]")
    elseif(APPLE)
      # on Mac we also know that blas and lapack are available
      set(LAPACK_LIBRARIES "-llapack -lblas")
      set_config_option(HAVE_BLAS "Blas[veclib]")
      set_config_option(HAVE_LAPACK "Lapack[veclib]")
    endif()

    if(NOT HAVE_BLAS OR NOT HAVE_LAPACK)
      # if we haven't found blas and lapack check for OpenBlas
      set(OPENBLAS_LIBS_REQUIRED openblas)
      find_all_libraries(LAPACK_LIBRARIES OPENBLAS_LIBS_REQUIRED "" "")
      if(LAPACK_LIBRARIES)
        set_config_option(HAVE_BLAS "Blas[openblas]")
        set_config_option(HAVE_LAPACK "Lapack[openblas]")
        find_library(GFORTRAN_LIB gfortran)
        if(GFORTRAN_LIB)
          list(APPEND LAPACK_LIBRARIES ${GFORTRAN_LIB})
        endif()
      endif()
    endif()

    if(NOT HAVE_BLAS OR NOT HAVE_LAPACK)
      # if we still haven't found blas and lapack, use the standard cmake tests,
      # which require a working Fortran compiler
      enable_language(Fortran)
      find_package(BLAS)
      if(BLAS_FOUND)
        set_config_option(HAVE_BLAS "Blas")
        find_package(LAPACK)
        if(LAPACK_FOUND)
          set_config_option(HAVE_LAPACK "Lapack")
        else()
          set(LAPACK_LIBRARIES ${BLAS_LIBRARIES})
        endif()
        if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
          if(CMAKE_Fortran_COMPILER MATCHES "gfortran")
            list(APPEND LAPACK_LIBRARIES gfortran)
          elseif(CMAKE_Fortran_COMPILER MATCHES "f95")
            list(APPEND LAPACK_LIBRARIES gfortran)
          elseif(CMAKE_Fortran_COMPILER MATCHES "g77")
            list(APPEND LAPACK_LIBRARIES g2c)
          endif()
        elseif(CMAKE_Fortran_COMPILER MATCHES "pgi")
          list(APPEND LAPACK_LIBRARIES -pgf77libs)
        endif()
      endif()
    endif()
  endif()
endif()

if(ENABLE_TCMALLOC)
  find_library(TCMALLOC tcmalloc)
  if(TCMALLOC)
    set_config_option(HAVE_TCMALLOC "TCMalloc")
    list(APPEND EXTERNAL_LIBRARIES ${TCMALLOC})
  endif()
endif()

add_subdirectory(src/common)
add_subdirectory(src/numeric)
add_subdirectory(src/geo)

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/mesh AND ENABLE_MESH)
  add_subdirectory(src/mesh)
  set_config_option(HAVE_MESH "Mesh")
endif()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/solver AND ENABLE_SOLVER)
  add_subdirectory(src/solver)
  set_config_option(HAVE_SOLVER "Solver")
endif()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/post AND ENABLE_POST)
  add_subdirectory(src/post)
  set_config_option(HAVE_POST "Post")
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/plugin AND ENABLE_PLUGINS)
    add_subdirectory(src/plugin)
    set_config_option(HAVE_PLUGINS "Plugins")
  endif()
endif()

if(ENABLE_ANN)
  find_library(ANN_LIB ANN PATH_SUFFIXES lib)
  find_path(ANN_INC "ANN.h" PATH_SUFFIXES src include ANN)
  if(ENABLE_SYSTEM_CONTRIB AND ANN_LIB AND ANN_INC)
    message(STATUS "Using system version of ANN")
    list(APPEND EXTERNAL_LIBRARIES ${ANN_LIB})
    list(APPEND EXTERNAL_INCLUDES ${ANN_INC})
    set_config_option(HAVE_ANN "ANN")
  elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/ANN)
    add_subdirectory(contrib/ANN)
    include_directories(contrib/ANN/include)
    set_config_option(HAVE_ANN "ANN[contrib]")
  endif()
endif()

if(ENABLE_ALGLIB)
  find_library(ALGLIB_LIB alglib)
  find_path(ALGLIB_INC "stdafx.h" PATH_SUFFIXES libalglib)
  if(ENABLE_SYSTEM_CONTRIB AND ALGLIB_LIB AND ALGLIB_INC)
    list(APPEND EXTERNAL_LIBRARIES ${ALGLIB_LIB})
    list(APPEND EXTERNAL_INCLUDES ${ALGLIB_INC})
    set_config_option(HAVE_ALGLIB "ALGLIB")
  elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/ALGLIB)
    add_subdirectory(contrib/ALGLIB)
    include_directories(contrib/ALGLIB)
    set_config_option(HAVE_ALGLIB "ALGLIB[contrib]")
  endif()
endif()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/DiscreteIntegration AND
   ENABLE_DINTEGRATION)
  add_subdirectory(contrib/DiscreteIntegration)
  include_directories(contrib/DiscreteIntegration)
  set_config_option(HAVE_DINTEGRATION "DIntegration")
endif()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/kbipack AND ENABLE_KBIPACK)
  set_config_option(HAVE_KBIPACK "Kbipack")
  add_subdirectory(contrib/kbipack)
  include_directories(contrib/kbipack)
  if(ENABLE_GMP)
    find_library(GMP_LIB gmp)
    find_path(GMP_INC "gmp.h" PATH_SUFFIXES src include)
  endif()
  if(GMP_LIB AND GMP_INC)
    set_config_option(HAVE_GMP "GMP")
    list(APPEND EXTERNAL_LIBRARIES ${GMP_LIB})
    list(APPEND EXTERNAL_INCLUDES ${GMP_INC})
  else()
    message(STATUS "GMP not found: Kbipack uses long int")
  endif()
endif()

if(ENABLE_MATHEX)
  find_library(MATHEX_LIB mathex PATH_SUFFIXES lib)
  find_path(MATHEX_INC "mathex.h" PATH_SUFFIXES src include)
  if(ENABLE_SYSTEM_CONTRIB AND MATHEX_LIB AND MATHEX_INC)
    list(APPEND EXTERNAL_LIBRARIES ${MATHEX_LIB})
    list(APPEND EXTERNAL_INCLUDES ${MATHEX_INC})
    set_config_option(HAVE_MATHEX "MathEx")
  elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/MathEx)
    add_subdirectory(contrib/MathEx)
    include_directories(contrib/MathEx)
    set_config_option(HAVE_MATHEX "MathEx[contrib]")
  endif()
endif()

if(ENABLE_TINYXML2)
  find_library(TINYXML2_LIB mathex PATH_SUFFIXES lib)
  find_path(TINYXML2_INC "tinyxml2.h" PATH_SUFFIXES src include)
  if(ENABLE_SYSTEM_CONTRIB AND TINYXML2_LIB AND TINYXML2_INC)
    list(APPEND EXTERNAL_LIBRARIES ${TINYXML2_LIB})
    list(APPEND EXTERNAL_INCLUDES ${TINYXML2_INC})
    set_config_option(HAVE_TINYXML2 "TinyXML2")
  elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/tinyxml2)
    add_subdirectory(contrib/tinyxml2)
    include_directories(contrib/tinyxml2)
    set_config_option(HAVE_TINYXML2 "TinyXML2[contrib]")
  endif()
endif()

if(ENABLE_MPI)
  find_package(MPI)
  if(MPI_FOUND)
    set_config_option(HAVE_MPI "MPI")
    list(APPEND EXTERNAL_INCLUDES ${MPI_CXX_INCLUDE_PATH})
    list(APPEND EXTERNAL_LIBRARIES ${MPI_CXX_LIBRARIES})
    set(CMAKE_C_COMPILER ${MPI_C_COMPILER})
    set(CMAKE_CXX_COMPILER ${MPI_CXX_COMPILER})
    set(CMAKE_Fortran_COMPILER ${MPI_Fortran_COMPILER})
  endif()
endif()

if(ENABLE_MESQUITE)
  find_library(MESQUITE_LIB mesquite)
  find_path(MESQUITE_INC "Mesquite.hpp" PATH_SUFFIXES src include)
  if(MESQUITE_LIB AND MESQUITE_INC)
    set_config_option(HAVE_MESQUITE "Mesquite")
    list(APPEND EXTERNAL_LIBRARIES ${MESQUITE_LIB})
    list(APPEND EXTERNAL_INCLUDES ${MESQUITE_INC})
  endif()
endif()

if(ENABLE_P4EST)
  find_library(P4EST_LIB p4est)
  find_path(P4EST_INC "p4est.h" PATH_SUFFIXES src)
  find_library(SC_LIB sc)
  if(P4EST_LIB AND P4EST_INC AND SC_LIB)
    set_config_option(HAVE_P4EST "P4est")
    list(APPEND EXTERNAL_LIBRARIES ${P4EST_LIB} ${SC_LIB})
    list(APPEND EXTERNAL_INCLUDES ${P4EST_INC})
  endif()
endif()

if(HAVE_MESH OR HAVE_SOLVER)
  if(ENABLE_METIS)
    find_library(METIS_LIB metis PATH_SUFFIXES lib)
    find_path(METIS_INC "metis.h" PATH_SUFFIXES include)
    if(ENABLE_SYSTEM_CONTRIB AND METIS_LIB AND METIS_INC)
      message(STATUS "Using system version of METIS")
      list(APPEND EXTERNAL_LIBRARIES ${METIS_LIB})
      list(APPEND EXTERNAL_INCLUDES ${METIS_INC})
      set_config_option(HAVE_METIS "Metis")
    elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/metis)
      add_definitions(-DUSE_GKREGEX)
      add_subdirectory(contrib/metis)
      include_directories(contrib/metis/include contrib/metis/libmetis
                          contrib/metis/GKlib)
      set_config_option(HAVE_METIS "Metis[contrib]")
    endif()
  endif()
endif()

if(HAVE_MESH)
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/mesh/tetgenBR.cxx)
    set_config_option(HAVE_TETGENBR "TetGen/BR")
  endif()

  if(ENABLE_VOROPP)
    find_library(VOROPP_LIB voro++)
    find_path(VOROPP_INC "voro++.hh" PATH_SUFFIXES voro++)
    if(ENABLE_SYSTEM_CONTRIB AND VOROPP_LIB AND VOROPP_INC)
      message(STATUS "Using system version of voro++")
      list(APPEND EXTERNAL_LIBRARIES ${VOROPP_LIB})
      list(APPEND EXTERNAL_INCLUDES ${VOROPP_INC})
      set_config_option(HAVE_VOROPP "Voro++")
    elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/voro++)
      add_subdirectory(contrib/voro++)
      include_directories(contrib/voro++/src)
      set_config_option(HAVE_VOROPP "Voro++[contrib]")
    endif()
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/HighOrderMeshOptimizer AND
     EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/MeshOptimizer AND
     EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/MeshQualityOptimizer AND
     ENABLE_OPTHOM)
    add_subdirectory(contrib/HighOrderMeshOptimizer)
    include_directories(contrib/HighOrderMeshOptimizer)
    add_subdirectory(contrib/MeshOptimizer)
    include_directories(contrib/MeshOptimizer)
    include_directories(${CMAKE_CURRENT_BINARY_DIR}/contrib/MeshOptimizer)
    add_subdirectory(contrib/MeshQualityOptimizer)
    include_directories(contrib/MeshQualityOptimizer)
    set_config_option(HAVE_OPTHOM "OptHom")
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/domhex AND ENABLE_DOMHEX)
    add_subdirectory(contrib/domhex)
    include_directories(contrib/domhex)
    set_config_option(HAVE_DOMHEX "DomHex")
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/QuadTri AND ENABLE_QUADTRI)
    add_subdirectory(contrib/QuadTri)
    include_directories(contrib/QuadTri)
    set_config_option(HAVE_QUADTRI "QuadTri")
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/blossom AND ENABLE_BLOSSOM)
    add_subdirectory(contrib/blossom)
    include_directories(contrib/blossom/MATCH contrib/blossom/concorde97
                        contrib/blossom/concorde97/INCLUDE)
    set_config_option(HAVE_BLOSSOM "Blossom")
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/nii2mesh AND ENABLE_NII2MESH)
    add_subdirectory(contrib/nii2mesh)
    include_directories(contrib/nii2mesh)
    set_config_option(HAVE_NII2MESH "Nii2mesh")
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/untangle AND ENABLE_UNTANGLE)
    add_subdirectory(contrib/untangle)
    include_directories(contrib/untangle)
    set_config_option(HAVE_UNTANGLE "Untangle")
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/Netgen AND ENABLE_NETGEN)
    add_subdirectory(contrib/Netgen)
    include_directories(contrib/Netgen contrib/Netgen/libsrc/include
                        contrib/Netgen/nglib)
    set_config_option(HAVE_NETGEN "Netgen")
    add_definitions(-DNO_PARALLEL_THREADS -DNOTCL)
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/bamg AND ENABLE_BAMG)
    add_subdirectory(contrib/bamg)
    include_directories(contrib/bamg contrib/bamg/bamglib)
    set_config_option(HAVE_BAMG "Bamg")
  endif()

  if(ENABLE_MMG)
    find_library(MMG_LIB NAMES Mmg mmg)
    find_path(MMG_INC "libmmg.h" PATH_SUFFIXES mmg)
    if(MMG_LIB AND MMG_INC)
      list(APPEND EXTERNAL_LIBRARIES ${MMG_LIB})
      list(APPEND EXTERNAL_INCLUDES ${MMG_INC})
      set_config_option(HAVE_MMG "Mmg")
    endif()
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/hxt AND ENABLE_HXT)
    add_subdirectory(contrib/hxt)
    include_directories(BEFORE ${HXT_INC_DIRS})
    set_config_option(HAVE_HXT "Hxt")
    # do not use arithmetic contraction in predicates.c
    if(MSVC OR (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND WIN32))
      set_source_files_properties(
          "${CMAKE_CURRENT_SOURCE_DIR}/contrib/hxt/predicates/src/predicates.c"
          PROPERTIES COMPILE_FLAGS "/fp:strict")
    elseif(CMAKE_C_COMPILER_ID STREQUAL "Intel")
      set_source_files_properties(
          "${CMAKE_CURRENT_SOURCE_DIR}/contrib/hxt/predicates/src/predicates.c"
          PROPERTIES COMPILE_FLAGS "-fp-model strict")
    elseif(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
      set_source_files_properties(
          "${CMAKE_CURRENT_SOURCE_DIR}/contrib/hxt/predicates/src/predicates.c"
          PROPERTIES COMPILE_FLAGS  "-fno-unsafe-math-optimizations -ffp-contract=off")
    # else()
    #   message(WARNING "Unknown compiler: make sure compiled functions from "
    #           "predicates.c do not use extended double precision and follow "
    #           "the IEEE754 standard. It is crucial for the robustness of "
    #           "geometric predicates.")
    endif()
    if(MSVC)
      add_definitions(/bigobj)
    endif()
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/QuadMeshingTools
     AND ENABLE_QUADMESHINGTOOLS)
    set_config_option(HAVE_QUADMESHINGTOOLS "QuadMeshingTools")
    add_subdirectory(contrib/QuadMeshingTools)
    include_directories(BEFORE contrib/QuadMeshingTools)
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/WinslowUntangler
     AND ENABLE_WINSLOWUNTANGLER)
    set_config_option(HAVE_WINSLOWUNTANGLER "WinslowUntangler")
    add_subdirectory(contrib/WinslowUntangler)
    include_directories(BEFORE contrib/WinslowUntangler)
  endif()
endif()

if(ENABLE_MED OR ENABLE_CGNS)
  find_package(HDF5)
  if(HDF5_FOUND)
    set(HDF5_LIB "${HDF5_C_LIBRARIES}")
    list(APPEND EXTERNAL_INCLUDES ${HDF5_INCLUDE_DIRS})
    if(ENABLE_MED)
      find_library(MED_LIB medC HINTS ENV MED3HOME PATH_SUFFIXES lib)
      find_path(MED_INC "med.h" HINTS ENV MED3HOME PATH_SUFFIXES include)
      if(MED_LIB AND MED_INC)
        set_config_option(HAVE_MED "Med")
        list(APPEND EXTERNAL_LIBRARIES ${MED_LIB})
        list(APPEND EXTERNAL_INCLUDES ${MED_INC})
      endif()
    endif()
    if(ENABLE_CGNS)
      find_library(CGNS_LIB cgns HINTS ENV CGNS_ROOT PATH_SUFFIXES lib)
      find_path(CGNS_INC "cgnslib.h" HINTS ENV CGNS_ROOT PATH_SUFFIXES include)
      if(CGNS_LIB AND CGNS_INC)
        set_config_option(HAVE_LIBCGNS "Cgns")
        list(APPEND EXTERNAL_LIBRARIES ${CGNS_LIB})
        list(APPEND EXTERNAL_INCLUDES ${CGNS_INC})
        if(ENABLE_CGNS_CPEX0045)
          set_config_option(HAVE_LIBCGNS_CPEX0045 "Cgns_CPEX0045")
        endif()
      endif()
    endif()
    if(MED_LIB OR CGNS_LIB)
      list(APPEND EXTERNAL_LIBRARIES ${HDF5_LIB})
      find_library(SZ_LIB NAMES szlib sz)
      if(SZ_LIB)
        list(APPEND EXTERNAL_LIBRARIES ${SZ_LIB})
      endif()
      if(NOT HAVE_LIBZ) # necessary for non-GUI builds
        find_package(ZLIB)
        if(ZLIB_FOUND)
          set_config_option(HAVE_LIBZ "Zlib")
          list(APPEND EXTERNAL_LIBRARIES ${ZLIB_LIBRARIES})
        endif()
      endif()
    endif()
  else()
    message(STATUS "HDF5 not found")
  endif()
endif()

if(HAVE_SOLVER)
  if(ENABLE_GMM)
    find_path(GMM_INC "gmm.h" PATH_SUFFIXES src include include/gmm)
    if(ENABLE_SYSTEM_CONTRIB AND GMM_INC)
      message(STATUS "Using system version of GMM")
      list(APPEND EXTERNAL_INCLUDES ${GMM_INC})
      set_config_option(HAVE_GMM "Gmm")
    elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/gmm)
      include_directories(contrib/gmm)
      set_config_option(HAVE_GMM "Gmm[contrib]")
    endif()
  endif()

  if(ENABLE_PETSC)
    if(PETSC_DIR)
      set(ENV_PETSC_DIR ${PETSC_DIR})
    else()
      set(ENV_PETSC_DIR $ENV{PETSC_DIR})
    endif()
    if(PETSC_ARCH)
      set(ENV_PETSC_ARCH ${PETSC_ARCH})
    else()
      set(ENV_PETSC_ARCH $ENV{PETSC_ARCH})
    endif()
    set(PETSC_POSSIBLE_CONF_FILES
        ${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/conf/petscvariables
        ${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/lib/petsc-conf/petscvariables
        ${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/lib/petsc/conf/petscvariables)
    foreach(FILE ${PETSC_POSSIBLE_CONF_FILES})
      if(EXISTS ${FILE})
        # old-style PETSc installations (using PETSC_DIR and PETSC_ARCH)
        message(STATUS "Using PETSc dir: ${ENV_PETSC_DIR}")
        message(STATUS "Using PETSc arch: ${ENV_PETSC_ARCH}")
        # find includes by parsing the petscvariables file
        file(STRINGS ${FILE} PETSC_VARIABLES NEWLINE_CONSUME)
      endif()
    endforeach()
    if(PETSC_VARIABLES)
      # try to find PETSC_CC_INCLUDES for PETSc >= 3.4
      string(REGEX MATCH "PETSC_CC_INCLUDES = [^\n\r]*" PETSC_PACKAGES_INCLUDES
             ${PETSC_VARIABLES})
      if(PETSC_PACKAGES_INCLUDES)
        string(REPLACE "PETSC_CC_INCLUDES = " "" PETSC_PACKAGES_INCLUDES
               ${PETSC_PACKAGES_INCLUDES})
      else()
        # try to find PETSC_PACKAGES_INCLUDES in older versions
        list(APPEND EXTERNAL_INCLUDES ${ENV_PETSC_DIR}/include)
        list(APPEND EXTERNAL_INCLUDES ${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/include)
        string(REGEX MATCH "PACKAGES_INCLUDES = [^\n\r]*" PETSC_PACKAGES_INCLUDES
               ${PETSC_VARIABLES})
        string(REPLACE "PACKAGES_INCLUDES = " "" PETSC_PACKAGES_INCLUDES
               ${PETSC_PACKAGES_INCLUDES})
      endif()
      if(PETSC_PACKAGES_INCLUDES)
        if(PETSC_PACKAGES_INCLUDES)
          string(REPLACE "-I" "" PETSC_PACKAGES_INCLUDES ${PETSC_PACKAGES_INCLUDES})
          string(REPLACE " " ";" PETSC_PACKAGES_INCLUDES ${PETSC_PACKAGES_INCLUDES})
          foreach(VAR ${PETSC_PACKAGES_INCLUDES})
            # seem to include unexisting directories (/usr/include/lib64)
            # check to avoid warnings
            if(EXISTS ${VAR})
              list(APPEND EXTERNAL_INCLUDES ${VAR})
            endif()
          endforeach()
        endif()
      endif()
      # find libraries (<= 3.0)
      set(PETSC_LIBS_REQUIRED petscksp petscdm petscmat petscvec petsc)
      find_all_libraries(PETSC_LIBS PETSC_LIBS_REQUIRED
                         ${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/lib "")
      # petsc 3.1 creates only one library (libpetsc)
      if(NOT PETSC_LIBS)
        find_library(PETSC_LIBS petsc PATHS ${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/lib
                     NO_DEFAULT_PATH)
      endif()
      if(PETSC_LIBS)
        set_config_option(HAVE_PETSC "PETSc")
        if(NOT HAVE_BLAS)
          set_config_option(HAVE_BLAS "Blas[petsc]")
        endif()
        if(NOT HAVE_LAPACK)
          set_config_option(HAVE_LAPACK "Lapack[petsc]")
        endif()
      endif()
      # find slepc (needs to be linked in before petsc)
      if(ENABLE_SLEPC)
        if(SLEPC_DIR)
          set(ENV_SLEPC_DIR ${SLEPC_DIR})
         else()
          set(ENV_SLEPC_DIR $ENV{SLEPC_DIR})
        endif()
        find_library(SLEPC_LIB slepc PATHS ${ENV_SLEPC_DIR}/${ENV_PETSC_ARCH}/lib
                     NO_DEFAULT_PATH)
        if(SLEPC_LIB)
          find_path(SLEPC_INC "slepc.h" PATHS ${ENV_SLEPC_DIR} PATH_SUFFIXES include
                    ${ENV_PETSC_ARCH}/include include/slepc NO_DEFAULT_PATH)
          if(SLEPC_INC)
            message(STATUS "Using SLEPc dir: ${ENV_SLEPC_DIR}")
            set_config_option(HAVE_SLEPC "SLEPc")
            list(APPEND EXTERNAL_LIBRARIES ${SLEPC_LIB})
            list(APPEND EXTERNAL_INCLUDES ${SLEPC_INC})
            find_path(SLEPC_INC2 "slepcconf.h" PATHS ${ENV_SLEPC_DIR}
                      PATH_SUFFIXES ${ENV_PETSC_ARCH}/include NO_DEFAULT_PATH)
            if(SLEPC_INC2)
              list(APPEND EXTERNAL_INCLUDES ${SLEPC_INC2})
            endif()
          endif()
        endif()
      endif()
      list(APPEND EXTERNAL_LIBRARIES ${PETSC_LIBS})
      # find additional libraries to link with
      string(REGEX MATCH "PACKAGES_LIBS = [^\n\r]*" PLIBS ${PETSC_VARIABLES})
      if(PLIBS)
        string(REPLACE "PACKAGES_LIBS = " "" PLIBS ${PLIBS})
        string(STRIP ${PLIBS} PLIBS)
        list(APPEND EXTERNAL_LIBRARIES "${PLIBS}")
      endif()
      string(REGEX MATCH "PETSC_EXTERNAL_LIB_BASIC = [^\n\r]*" PLIBS_BASIC ${PETSC_VARIABLES})
      if(PLIBS_BASIC)
        string(REPLACE "PETSC_EXTERNAL_LIB_BASIC = " "" PLIBS_BASIC ${PLIBS_BASIC})
        string(STRIP ${PLIBS_BASIC} PLIBS_BASIC)
        separate_arguments(PLIBS_BASIC)
        list(APPEND EXTERNAL_LIBRARIES "${PLIBS_BASIC}")
      endif()
      string(REGEX MATCH "PCC_LINKER_LIBS = [^\n\r]*" LLIBS ${PETSC_VARIABLES})
      if(LLIBS)
        string(REPLACE "PCC_LINKER_LIBS = " "" LLIBS ${LLIBS})
        string(STRIP ${LLIBS} LLIBS)
        list(APPEND EXTERNAL_LIBRARIES "${LLIBS}")
      endif()
    else()
      # new-style PETSc installations (in standard system directories)
      find_library(PETSC_LIBS petsc)
      find_path(PETSC_INC "petsc.h" PATH_SUFFIXES include/petsc)
      if(PETSC_LIBS AND PETSC_INC)
        set_config_option(HAVE_PETSC "PETSc")
        if(ENABLE_SLEPC)
          find_library(SLEPC_LIB slepc)
          find_path(SLEPC_INC "slepc.h" PATH_SUFFIXES include/slepc)
          if(SLEPC_LIB AND SLEPC_INC)
            set_config_option(HAVE_SLEPC "SLEPc")
            list(APPEND EXTERNAL_LIBRARIES ${SLEPC_LIB})
            list(APPEND EXTERNAL_INCLUDES ${SLEPC_INC})
          endif()
        endif()
        list(APPEND EXTERNAL_LIBRARIES ${PETSC_LIBS})
        list(APPEND EXTERNAL_INCLUDES ${PETSC_INC})
      endif()
    endif()
  endif()

  # if(ENABLE_MUMPS AND HAVE_BLAS AND HAVE_LAPACK)
  #   set(MUMPS_LIBS_REQUIRED smumps dmumps cmumps zmumps mumps_common pord)
  #   if(NOT ENABLE_MPI)
  #     list(APPEND MUMPS_LIBS_REQUIRED mpiseq)
  #   endif()
  #   find_all_libraries(MUMPS_LIBRARIES MUMPS_LIBS_REQUIRED "" "lib")
  #   find_path(SMUMPS_INC "smumps_c.h" PATH_SUFFIXES src include)
  #   find_path(DMUMPS_INC "dmumps_c.h" PATH_SUFFIXES src include)
  #   find_path(CMUMPS_INC "cmumps_c.h" PATH_SUFFIXES src include)
  #   find_path(ZMUMPS_INC "zmumps_c.h" PATH_SUFFIXES src include)
  #   if(MUMPS_LIBRARIES AND SMUMPS_INC AND DMUMPS_INC AND CMUMPS_INC AND ZMUMPS_INC)
  #     set_config_option(HAVE_MUMPS "MUMPS")
  #     list(APPEND EXTERNAL_LIBRARIES ${MUMPS_LIBRARIES})
  #     list(APPEND EXTERNAL_INCLUDES ${SMUMPS_INC})
  #     list(APPEND EXTERNAL_INCLUDES ${DMUMPS_INC})
  #     list(APPEND EXTERNAL_INCLUDES ${CMUMPS_INC})
  #     list(APPEND EXTERNAL_INCLUDES ${ZMUMPS_INC})
  #     find_library(GFORTRAN_LIB gfortran)
  #     if(GFORTRAN_LIB)
  #       list(APPEND EXTERNAL_LIBRARIES ${GFORTRAN_LIB})
  #     endif()
  #     if(ENABLE_GMM) # use GMM/MUMPS interface
  #       add_definitions(-DGMM_USES_MUMPS)
  #     endif()
  #   endif()
  # endif()

  if(ENABLE_GETDP)
    find_library(GETDP_LIB GetDP)
    find_path(GETDP_INC "GetDP.h" PATH_SUFFIXES getdp)
    if(GETDP_LIB AND GETDP_INC)
      set_config_option(HAVE_GETDP "GetDP")
      list(APPEND EXTERNAL_LIBRARIES ${GETDP_LIB})
      list(APPEND EXTERNAL_INCLUDES ${GETDP_INC})
    endif()
  endif()
endif()

# if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/pro AND ENABLE_PRO)
#   add_subdirectory(pro)
# endif()

# if(ENABLE_ZIPPER)
#   # if(ENABLE_BUILD_IOS)
#   #   set_config_option(HAVE_LIBZ "Zlib")
#   # endif()
#   if(NOT HAVE_LIBZ) # necessary for non-GUI builds
#     find_package(ZLIB)
#     if(ZLIB_FOUND)
#       set_config_option(HAVE_LIBZ "Zlib")
#       list(APPEND EXTERNAL_LIBRARIES ${ZLIB_LIBRARIES})
#       list(APPEND EXTERNAL_INCLUDES ${ZLIB_INCLUDE_DIR})
#     endif()
#   endif()
#   if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/zipper AND HAVE_LIBZ)
#     add_subdirectory(contrib/zipper)
#     include_directories(contrib/zipper)
#     set_config_option(HAVE_ZIPPER "Zipper")
#   endif()
# endif()

check_symbol_exists(vsnprintf "stdio.h" HAVE_VSNPRINTF)
if(NOT HAVE_VSNPRINTF AND NOT ENABLE_BUILD_IOS AND NOT ENABLE_BUILD_ANDROID)
  set_config_option(HAVE_NO_VSNPRINTF "NoVsnprintf")
endif()

check_include_file(sys/socket.h HAVE_SYS_SOCKET_H)
if(HAVE_SYS_SOCKET_H)
  set(CMAKE_EXTRA_INCLUDE_FILES sys/socket.h)
endif()
check_type_size(socklen_t SOCKLEN_T_SIZE)
set(CMAKE_EXTRA_INCLUDE_FILES)
if(NOT SOCKLEN_T_SIZE AND NOT ENABLE_BUILD_IOS AND NOT ENABLE_BUILD_ANDROID)
  set_config_option(HAVE_NO_SOCKLEN_T "NoSocklenT")
endif()

check_include_file(stdint.h HAVE_STDINT_H)
if(HAVE_STDINT_H)
  set(CMAKE_EXTRA_INCLUDE_FILES stdint.h)
else()
  set_config_option(HAVE_NO_STDINT_H "NoStdintH")
endif()
check_type_size(intptr_t INTPTR_T_SIZE)
set(CMAKE_EXTRA_INCLUDE_FILES)
if(NOT INTPTR_T_SIZE AND NOT ENABLE_BUILD_IOS AND NOT ENABLE_BUILD_ANDROID)
  set_config_option(HAVE_NO_INTPTR_T "NoIntptrT")
endif()

check_include_file(dlfcn.h DLFCN_H)
if(DLFCN_H)
  set_config_option(HAVE_DLOPEN "Dlopen")
  list(APPEND EXTERNAL_LIBRARIES ${CMAKE_DL_LIBS})
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  check_include_file(linux/joystick.h LINUX_JOYSTICK_H)
  if(LINUX_JOYSTICK_H)
    set_config_option(HAVE_LINUX_JOYSTICK "LinuxJoystick")
  endif()
endif()

if(WIN32)
  add_definitions(-D_USE_MATH_DEFINES)
  list(APPEND EXTERNAL_LIBRARIES winmm wsock32 ws2_32 psapi)
endif()

if(MSVC)
  add_definitions(-DNOMINMAX -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS
                  -D_SCL_SECURE_NO_DEPRECATE)
endif()

# add C functions API
set(CMSH_SRC ${CMSH_SRC};src/cmshc.cpp)

# force full warnings to encourage everybody to write clean(er) code
check_cxx_compiler_flag("-Wall" WALL)
if(WALL AND NOT MSVC)
  file(GLOB_RECURSE WALL_SRC src/common/*.cpp FunctionSpace/*.cpp
       src/geo/*.cpp src/graphics/*.cpp src/mesh/*.cpp src/numeric/*.cpp
       src/plugin/*.cpp src/post/*.cpp src/solver/*.cpp)
  set(WF "-Wall")
  check_cxx_compiler_flag("-Wint-to-void-pointer-cast" WCAST)
  if(WCAST)
    set(WF "${WF} -Wno-int-to-void-pointer-cast")
  endif()
  check_cxx_compiler_flag("-Wdeprecated-declarations" WDEPREC)
  if(WDEPREC)
    # FIXME: remove this when we have fixed the deprecated GLU code for OpenGL3
    set(WF "${WF} -Wno-deprecated-declarations")
  endif()
  check_cxx_compiler_flag("-Wmisleading-indentation" WIND)
  if(WIND)
    set(WF "${WF} -Wno-misleading-indentation")
  endif()
  check_cxx_compiler_flag("-Wno-attributes" WATTR)
  if(WATTR)
    # FIXME: remove this when GCC behaves more intelligently
    set(WF "${WF} -Wno-attributes")
  endif()
  check_cxx_compiler_flag("-Wno-unused-but-set-variable" WUNUSED)
  if(WUNUSED)
    set(WF "${WF} -Wno-unused-but-set-variable")
  endif()
  if(WIN32 OR NOT HAVE_64BIT_SIZE_T)
    # FIXME: remove printf family warnings on 32 bit systems and windows
    check_cxx_compiler_flag("-Wno-format" WFORMAT)
    if(WFORMAT)
      set(WF "${WF} -Wno-format")
    endif()
  endif()
  set_compile_flags(WALL_SRC ${WF})
endif()

# don't issue warnings for contributed libraries and for autogenerated files
check_cxx_compiler_flag("-w" NOWARN)
if(NOWARN)
  file(GLOB_RECURSE NOWARN_SRC contrib/*.cpp contrib/*.cc contrib/*.cxx
       contrib/*.c)
  set_compile_flags(NOWARN_SRC "-w")
endif()

# fine-tune optimization flags for particular files
check_cxx_compiler_flag("-O0" NOOPT)
if(NOOPT OR ENABLE_BUILD_IOS)
  file(GLOB_RECURSE NOOPT_SRC contrib/bamg/*)
  set_compile_flags(NOOPT_SRC "-O0")
endif()

if(MSVC OR (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND WIN32))
  set_source_files_properties(
    "${CMAKE_CURRENT_SOURCE_DIR}/src/numeric/robustPredicates.cpp"
    PROPERTIES COMPILE_FLAGS "/fp:strict")
elseif(CMAKE_C_COMPILER_ID STREQUAL "Intel")
  set_source_files_properties(
    "${CMAKE_CURRENT_SOURCE_DIR}/src/numeric/robustPredicates.cpp"
    PROPERTIES COMPILE_FLAGS "-fp-model strict")
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
  set_source_files_properties(
    "${CMAKE_CURRENT_SOURCE_DIR}/src/numeric/robustPredicates.cpp"
    PROPERTIES COMPILE_FLAGS "-fno-unsafe-math-optimizations -ffp-contract=off")
# else()
#   message(WARNING "Unknown compiler: make sure compiled functions from "
#           "src/numeric/robustPredicates.cpp do not use extended double precision "
#           "and follow the IEEE754 standard. It is crucial for the robustness of "
#           "geometric predicates.")
endif()

# enable Revoropt and set compile flags for the corresponding plugin
if(ENABLE_REVOROPT)
  if(HAVE_EIGEN AND HAVE_MESH AND HAVE_PLUGINS AND HAVE_ANN AND HAVE_ALGLIB)
    list(APPEND EXTERNAL_INCLUDES contrib/Revoropt/include)
    set_config_option(HAVE_REVOROPT "Revoropt")
    add_definitions(-DUSE_ANN)
  else()
    message(WARNING "Revoropt requires Eigen, Mesh, Plugins, ANN and ALGLIB")
  endif()
endif()

if(HAVE_MESH AND NOT HAVE_EIGEN AND NOT HAVE_LAPACK)
  message(WARNING "Most meshing algorithms will not be functional without "
          "Eigen or Lapack")
endif()

list(SORT CONFIG_OPTIONS)
set(CMSH_CONFIG_OPTIONS "")
foreach(OPT ${CONFIG_OPTIONS})
  set(CMSH_CONFIG_OPTIONS "${CMSH_CONFIG_OPTIONS} ${OPT}")
endforeach()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/common/CmshConfig.h.in
               ${CMAKE_CURRENT_BINARY_DIR}/src/common/CmshConfig.h)

# process cmake environment variables so we can append them to the -I include
# commands. This is not recommended (we should only use the cache variables) but
# it is very convenient: otherwise we have to remember providing the
# -D... options to cmake for each new build.
set(ENV_CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
set(ENV_CMAKE_INCLUDE_PATH $ENV{CMAKE_INCLUDE_PATH})
if(UNIX)
  if(ENV_CMAKE_PREFIX_PATH)
    string(REPLACE ":" ";" ENV_CMAKE_PREFIX_PATH ${ENV_CMAKE_PREFIX_PATH})
  endif()
  if(ENV_CMAKE_INCLUDE_PATH)
    string(REPLACE ":" ";" ENV_CMAKE_INCLUDE_PATH ${ENV_CMAKE_INCLUDE_PATH})
  endif()
endif()
list(APPEND EXTERNAL_INCLUDES ${CMAKE_INCLUDE_PATH} ${ENV_CMAKE_INCLUDE_PATH})
list(APPEND EXTERNAL_INCLUDES ${CMAKE_PREFIX_PATH} ${ENV_CMAKE_PREFIX_PATH})
foreach(DIR ${CMAKE_PREFIX_PATH} ${ENV_CMAKE_PREFIX_PATH})
  list(APPEND EXTERNAL_INCLUDES ${DIR}/include)
endforeach()

if(EXTERNAL_INCLUDES)
  list(REMOVE_DUPLICATES EXTERNAL_INCLUDES)
endif()

set(LINK_LIBRARIES ${EXTERNAL_LIBRARIES} ${LAPACK_LIBRARIES})

# try to use static gfortran on static builds (cannot do this on dynamic builds
# as e.g. Debian compiles libgfortran.a without -fPIC: sigh...)
# if(NOT ENABLE_BUILD_DYNAMIC AND NOT ENABLE_BUILD_SHARED)
#   find_library(GFORTRAN_STATIC libgfortran.a)
#   if(GFORTRAN_STATIC)
#     set(CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES)
#     message(STATUS "Using static libgfortran")
#     foreach(STR ${LINK_LIBRARIES})
#       string(REPLACE "-lgfortran" ${GFORTRAN_STATIC} STR2 ${STR})
#       list(APPEND LINK_LIBRARIES2 ${STR2})
#     endforeach()
#     set(LINK_LIBRARIES ${LINK_LIBRARIES2})
#   endif()
# endif()

# Linux-specific linker options
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  if(HAVE_OCC)
    find_library(RT_LIB rt)
    if(RT_LIB)
      list(APPEND LINK_LIBRARIES ${RT_LIB})
    endif()
  endif()
  if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Intel")
    add_definitions(-fPIC)
  endif()
endif()

# # we could specify include dirs more selectively, but this is simpler
include_directories(src/common src/geo src/graphics src/mesh src/solver
                    src/numeric src/plugin src/post include
                    ${EXTERNAL_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/src/common)

# set this for external codes that might include this CMakeList file
set(CMSH_EXTERNAL_INCLUDE_DIRS ${EXTERNAL_INCLUDES} CACHE
    STRING "External include directories" FORCE)
set(CMSH_EXTERNAL_LIBRARIES ${LINK_LIBRARIES} CACHE
    STRING "External libraries" FORCE)

# group sources for easier navigation in IDEs
foreach(DIR ${CMSH_DIRS})
  string(REGEX REPLACE "\\+" "\\\\+" DIR ${DIR})
  source_group(${DIR} REGULAR_EXPRESSION ${DIR}/.*)
endforeach()

# shared library target
# if(ENABLE_BUILD_SHARED OR ENABLE_BUILD_DYNAMIC)
add_library(shared SHARED ${CMSH_SRC})
set_target_properties(shared PROPERTIES OUTPUT_NAME cmsh)
if(WIN32)
  set_target_properties(shared PROPERTIES PREFIX "" IMPORT_PREFIX ""
    IMPORT_SUFFIX ".lib" COMPILE_FLAGS "-DCMSH_DLL -DCMSH_DLL_EXPORT")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  if(NOT ENABLE_PRIVATE_API)
    # hide all symbols by default (if not exporting the private API); symbols to
    # export should be marked with __attribute__ ((visibility("default")))
    set_target_properties(shared PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
  endif()
  if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    # TODO would be nice to have the equivalent on macOS
    message(STATUS "Excluding symbols from bundled static libraries")
    list(APPEND LINK_LIBRARIES "-Wl,--exclude-libs,ALL")
  endif()
endif()
target_link_libraries(shared ${LINK_LIBRARIES})

if(MSVC AND ENABLE_MSVC_STATIC_RUNTIME)
  message(STATUS "Note: By enabling ENABLE_MSVC_STATIC_RUNTIME, shared library "
          "won't link. In MSVC change /MT to /MD in the shared project properties")
endif()
# endif()

# OS specific linker options
if(WIN32 AND NOT MSVC)
  set(FLAGS "-Wl,--stack,16777216 -Wl,--image-base -Wl,0x10000000 -static")
  set(APPFLAGS "${FLAGS} -municode -mconsole")
  if(ENABLE_BUILD_DYNAMIC OR ENABLE_BUILD_SHARED)
    set_target_properties(shared PROPERTIES LINK_FLAGS "${FLAGS}")
  endif()
  # remove -Wl,-Bdynamic flags
  set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS)
  set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
endif()

if(UNIX)
  # cannot use cmake's file search functions here (they would only find files
  # existing at configuration time)
  add_custom_target(purge
                    COMMAND rm -f `find . -name *~ -o -name *~~`
                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  add_custom_target(etags
                    COMMAND etags `find . -name *.cpp -o -name *.h -o -name *.y`
                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
   CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
                  OUTPUT_VARIABLE CXX_COMPILER_VERSION
                  OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
  set(CXX_COMPILER_VERSION "Unknown")
endif()

set(LICENSE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.txt)

macro(unix2dos VARNAME UNIQUEPATH)
  file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/unix2dos/${UNIQUEPATH})
  set(UNIX2DOS_FILES)
  foreach(FILE ${${VARNAME}})
    file(READ ${FILE} F0)
    get_filename_component(N ${FILE} NAME)
    if(CYGWIN)
      string(REGEX REPLACE "\n" "\r\n" F1 "${F0}")
      file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/unix2dos/${UNIQUEPATH}/${N} "${F1}")
    else() # if not in Cygwin, cmake adds '\r's automatically
      file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/unix2dos/${UNIQUEPATH}/${N} "${F0}")
    endif()
    list(APPEND UNIX2DOS_FILES ${CMAKE_CURRENT_BINARY_DIR}/unix2dos/${UNIQUEPATH}/${N})
  endforeach()
  set(${VARNAME} ${UNIX2DOS_FILES})
endmacro()

if(ENABLE_BUILD_LIB)
  install(TARGETS lib DESTINATION ${CMSH_LIB} OPTIONAL)
endif()
if(ENABLE_BUILD_SHARED OR ENABLE_BUILD_DYNAMIC)
  install(TARGETS shared DESTINATION ${CMSH_LIB} OPTIONAL)
endif()

mark_as_advanced(ANN_INC ANN_LIB CAIRO_LIB CAIRO_INC CGNS_INC GMM_INC
                 GMP_INC GMP_LIB MMG_INC MMG_LIB HDF5_LIB
                 MED_LIB OCC_INC SZ_LIB
                 PETSC_LIBS SLEPC_INC SLEPC_INC2 SLEPC_LIB
                 CMSH_EXTERNAL_INCLUDE_DIRS CMSH_EXTERNAL_LIBRARIES)
